<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_mcs.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";   
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";

	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_cp();
	
	
	
$template = new template();





// FUNCTION // Display select suppliers instances
function display_home($message=''){


if($_POST['search']=='true'){
	 $_SESSION['search_mcs_instances']['prefix'] = $_POST['prefix'];
	 $_SESSION['search_mcs_instances']['hotel_name'] = $_POST['hotel_name'];
	 $_SESSION['search_mcs_instances']['resort'] = $_POST['resort'];
	 $_SESSION['search_mcs_instances']['tour_op'] = $_POST['tour_op'];
	 $_SESSION['search_mcs_instances']['airport_code'] = $_POST['airport_code'];
}

if($_GET['sort']){
	 $_SESSION['search_mcs_instances']['sort'] = $_GET['sort'];
	 $_SESSION['search_mcs_instances']['order'] = $_GET['order'];
}
	
	
$holidays = new holidays();
	
		
?>


<h2>Flagged Micros (MCS) Hotel Instances</h2>


<?
if($message){
	echo display_message($message);
}
?>


<div class="border_box">



<h3>Search Flagged Accommodation</h3>


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" >

<input type="hidden" name="search" value="true" />

<table class="libra">
<tr>
<td width="100">Prefix</td>
<td>
<select name="prefix">	
<option value=""></option>
<option value="a" <?= selected("a",$_SESSION['search_mcs_instances']['prefix']) ?>>a</option>
<option value="b" <?= selected("b",$_SESSION['search_mcs_instances']['prefix']) ?>>b</option>
<option value="c" <?= selected("c",$_SESSION['search_mcs_instances']['prefix']) ?>>c</option>
<option value="d" <?= selected("d",$_SESSION['search_mcs_instances']['prefix']) ?>>d</option>
<option value="e" <?= selected("e",$_SESSION['search_mcs_instances']['prefix']) ?>>e</option>
<option value="f" <?= selected("f",$_SESSION['search_mcs_instances']['prefix']) ?>>f</option>
<option value="g" <?= selected("g",$_SESSION['search_mcs_instances']['prefix']) ?>>g</option>
<option value="h" <?= selected("h",$_SESSION['search_mcs_instances']['prefix']) ?>>h</option>
<option value="i" <?= selected("i",$_SESSION['search_mcs_instances']['prefix']) ?>>i</option>
<option value="j" <?= selected("j",$_SESSION['search_mcs_instances']['prefix']) ?>>j</option>
<option value="k" <?= selected("k",$_SESSION['search_mcs_instances']['prefix']) ?>>k</option>
<option value="l" <?= selected("l",$_SESSION['search_mcs_instances']['prefix']) ?>>l</option>
<option value="m" <?= selected("m",$_SESSION['search_mcs_instances']['prefix']) ?>>m</option>
<option value="n" <?= selected("n",$_SESSION['search_mcs_instances']['prefix']) ?>>n</option>    
<option value="o" <?= selected("o",$_SESSION['search_mcs_instances']['prefix']) ?>>o</option>
<option value="p" <?= selected("p",$_SESSION['search_mcs_instances']['prefix']) ?>>p</option>
<option value="q" <?= selected("q",$_SESSION['search_mcs_instances']['prefix']) ?>>q</option>
<option value="r" <?= selected("r",$_SESSION['search_mcs_instances']['prefix']) ?>>r</option>    
<option value="s" <?= selected("s",$_SESSION['search_mcs_instances']['prefix']) ?>>s</option>
<option value="t" <?= selected("t",$_SESSION['search_mcs_instances']['prefix']) ?>>t</option>
<option value="u" <?= selected("u",$_SESSION['search_mcs_instances']['prefix']) ?>>u</option>
<option value="v" <?= selected("v",$_SESSION['search_mcs_instances']['prefix']) ?>>v</option>
<option value="w" <?= selected("w",$_SESSION['search_mcs_instances']['prefix']) ?>>w</option>
<option value="x" <?= selected("x",$_SESSION['search_mcs_instances']['prefix']) ?>>x</option>
<option value="y" <?= selected("y",$_SESSION['search_mcs_instances']['prefix']) ?>>y</option>
<option value="z" <?= selected("z",$_SESSION['search_mcs_instances']['prefix']) ?>>z</option>
</select>
</td>
</tr>
<tr>
<td>Hotel Name</td>
<td><input type="text" name="hotel_name" value="<?= $_SESSION['search_mcs_instances']['hotel_name'] ?>" /></td>
</tr>
<tr>
<td>Resort Name</td>
<td><input type="text" name="resort" value="<?= $_SESSION['search_mcs_instances']['resort'] ?>" /></td>
</tr>
<tr>
<td>Tour Op. code</td>
<td><input type="text" name="tour_op" value="<?= $_SESSION['search_mcs_instances']['tour_op'] ?>" /></td>
</tr>
<tr>
<td>Airport Code</td>
<td><input type="text" name="airport_code" value="<?= $_SESSION['search_mcs_instances']['airport_code'] ?>" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>


</form>

</div>

<br />



<?



	$results_order = return_alt('ASC', 'DESC', $_SESSION['search_mcs_instances']['order']);

	$search_params['prefix'] = $_SESSION['search_mcs_instances']['prefix'];
	$search_params['hotel_name'] = $_SESSION['search_mcs_instances']['hotel_name'];
	$search_params['resort'] = $_SESSION['search_mcs_instances']['resort'];
	$search_params['tour_op'] = $_SESSION['search_mcs_instances']['tour_op'];
	$search_params['airport_code'] = $_SESSION['search_mcs_instances']['airport_code'];
	$search_params['sort'] = $_SESSION['search_mcs_instances']['sort'];
	$search_params['order'] = $_SESSION['search_mcs_instances']['order'];
	
	$hotel_instances_mcs = new hotel_instances_mcs;
	$values = $hotel_instances_mcs->search_flagged_instances($search_params);





if(!empty($values)){

$instance_ids = array();

?>

<table class="aqua">
  <tr>
    <th width="200"><a href="<?= $_SERVER['PHP_SELF'] ?>?sort=hotel_name&order=<?= $results_order ?>">Name</a></th>
    <th width="200">Resort</th>
    <th width="150">Tour Operator Code</th>
	<th width="150"><a href="<?= $_SERVER['PHP_SELF'] ?>?sort=instance_date&order=<?= $results_order ?>">Date flagged</a></th>
    <th width="100">Airport</th>
	<th width="100">Actions</th>
</tr>

<? foreach($values as $value){ ?>
  <tr>
    <td><?= $value['hotel_name'] ?></td>
    <td><?= $value['resort'] ?></td>
    <td><?
	
		$params['tour_op_code'] = $value['tour_op'];
		$my_tour_op = $holidays->get_tour_operator($params);
		
		echo $value['tour_op']." (".$my_tour_op.")";
	
	
	
	?></td>
    <td><?= date('d/m/Y', $value['instance_date']) ?></td>
    <td><?= $value['destination_airport'] ?></td>
	<td><a href="<?= $_SERVER['PHP_SELF']."?action=view&instance_id=".$value['instance_id'] ?>">View</a></td>
</tr>
<?

$instance_ids[] = $value['instance_id'];

}

?>

</table>



<br />
<br />


<h3>Batch Process</h3>

<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" >

	<input type="hidden" name="action" value="batch_confirm" />
    <input type="hidden" name="batch" value="<?= implode(',', $instance_ids) ?>" />
    <table class="libra">
    <tr>
    <th width="100">Batch Process</th>
    <td><input type="text" name="tree_id" /></td>
    <td><input type="submit" /></td>
    </tr>
    </table>
    
    </form>
<?

}


}















// FUNCTION // Display details of the instance
function display_instance($values='', $form_errors=''){

    $hotel_instances_mcs = new hotel_instances_mcs();
	
	$params['instance_id'] = $_GET['instance_id'];
	$values = $hotel_instances_mcs->get_flagged_instance($params);
	$values = $values[0];
	unset($params);
	
	$holidays = new holidays();
	
	
?>


<h2>Hotel Flagged</h2>

<h3>Instance details</h3>

<?
if($message){
	echo display_message($message);
}
?>

<div class="border_box">



<fieldset><legend>Instance Details</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>hotel_name </strong></td>
    <td><?= $values['hotel_name'] ?></td>
  </tr>
  <tr>
    <td><strong>destination_airport</strong></td>
    <td><?= $values['destination_airport'] ?></td>
  </tr>
  <tr>
    <td><strong>tour_op</strong></td>
    <td><?
	
	
		$params['tour_op_code'] = $values['tour_op'];
		$my_tour_op = $holidays->get_tour_operator($params);
		
		echo $values['tour_op']." (".$my_tour_op.")";
	
	
	 ?></td>
  </tr>
  <tr>
    <td><strong>resort</strong></td>
    <td><?= $values['resort'] ?></td>
  </tr>

  <tr>
    <td><strong>travel_code</strong></td>
    <td><?= $values['travel_code'] ?></td>
</tr>
<tr>
    <td><strong>resort_code</strong></td>
    <td><?= $values['resort_code'] ?></td>
</tr>
<tr>
    <td><strong>holiday_code</strong></td>
    <td><?= $values['holiday_code'] ?></td>
</tr>
<tr>
    <td><strong>board_basis</strong></td>
    <td><?= $values['board_basis'] ?></td>
</tr>
<tr>
    <td><strong>hotel_code</strong></td>
    <td><?= $values['hotel_code'] ?></td>
</tr>
<tr>
    <td><strong>instance_date</strong></td>
    <td><?= date('d/m/y',$values['instance_date']) ?></td>
</tr>
</table>



<br />



<fieldset><legend>Link Instance</legend></fieldset>


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">

<input type="hidden" name="action" value="append_confirm" />
<input type="hidden" name="instance_id" value="<?= $values['instance_id'] ?>" />


<table class="libra">
  <tr>
    <td width="200"><strong>Link instance to tree Id.</strong></td>
    <td><input type="text" name="tree_id" value=""  /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" value="submit" name="submit" /></td>
  </tr>
</table>


</form>

</div>
<?



}






// FUNCTION // Display instance append confirmation form
function display_append_confirm(){

	$photos = new photos();	
	$tree = new tree();
	$hotel_instances_mcs = new hotel_instances_mcs();

	// Get hotel details
	$params['tree_id'] = $_GET['tree_id'];
	$values_acc = $tree->get_branch($params);
	$values_acc = $values_acc[0];
	unset($params);
	
	// Get instance details
	$params['instance_id'] = $_GET['instance_id'];
	$values_instance = $hotel_instances_mcs->get_flagged_instance($params);
	$values_instance = $values_instance[0];
	unset($params);
	
	
	
?>


<h2>Accommodation Flagged</h2>

<h3>Link Instance</h3>

<p>Please confirm that you want to link the following instances:</p>


<div class="border_box">



<fieldset><legend>Instance Details</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200">Name </td>
    <td><?= $values_instance['hotel_name'] ?></td>
  </tr>
  <tr>
    <td>destination_airport</td>
    <td><?= $values_instance['destination_airport'] ?></td>
  </tr>
  <tr>
    <td>tour_op</td>
    <td><?= $values_instance['tour_op'] ?></td>
  </tr>
  <tr>
    <td>resort</td>
    <td><?= $values_instance['resort'] ?></td>
  </tr>
  <tr>
    <td>travel_code</td>
    <td><?= $values_instance['travel_code'] ?></td>
</tr>
<tr>
    <td>resort_code</td>
    <td><?= $values_instance['resort_code'] ?></td>
</tr>
<tr>
    <td>holiday_code</td>
    <td><?= $values_instance['holiday_code'] ?></td>
</tr>
<tr>
    <td>board_basis</td>
    <td><?= $values_instance['board_basis'] ?></td>
</tr>
<tr>
    <td>hotel_code</td>
    <td><?= $values_instance['hotel_code'] ?></td>
</tr>
<tr>
    <td>instance_date</td>
    <td><?= date('d/m/y',$values['instance_date']) ?></td>
</tr>

</table>

<br />

<h3>With the following accommodation:</h3>


<fieldset><legend>Hotel Details</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200">Name</td>
    <td><?= $values_acc['name'] ?></td>
  </tr>
  <tr>
    <td>Description</td>
    <td><?= nl2br($values_acc['description']) ?></td>
  </tr>
  <tr>
    <td>Resort</td>
    <td><?
	
		$params['tree_id'] = $values_acc['parent_id'];
		$resort_values = $tree->get_branch($params);
		$resort_values = $resort_values[0];
		unset($params);
		echo $resort_values['name']." (".$resort_values['tree_id'].")";
	 
	 ?></td>
  </tr>
  
 
  <tr>
    <td>Photos</td>
    <td>

	<?
	// get photo url
	$lead_hotel_thumb_url = $tree->photo_url($values_acc['main_photo']); 
	
	
	
	?><img src="<?= $lead_hotel_thumb_url ?>" /></td>
  </tr>
  <tr>
    <td>Notes</td>
    <td <? if($values_acc['notes']) echo "style='border:1px solid #000'"; ?>><?= $values_acc['notes']; ?></td>
  </tr>
</table>

<br />


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">

	<input type="hidden" name="action" value="append" />
    
	<input type="hidden" name="instance_id" value="<?= $_GET['instance_id'] ?>" />
	<input type="hidden" name="tree_id" value="<?= $_GET['tree_id'] ?>" />

    <input type="button" value="Cancel" onClick="history.go(-1)" />
    <input type="submit" name="submit" value="Yes" class="form_button" /></td>


</form>

</div>

<?



}



// FUNCTION // Display instance batch confirmation form
function display_batch_confirm(){

	$destinations = new destinations();
	$photos = new photos();	
	$hotels = new hotels();
	$hotel_instances_mcs = new hotel_instances_mcs();

	// Get hotel details
	$params['tree_id'] = $_POST['tree_id'];
	$values_acc = $hotels->get_hotel($params);
	unset($params);

	
	
	
?>


<h2>Accommodation Flagged</h2>

<h3>Link Instance</h3>

<p>Please confirm that you want to link the following instances:</p>

<div class="border_box">



<?
	
	// Get instance details
	$batch = explode(',',$_POST['batch']);
	
	foreach($batch as $instance_item){

	// Get instance details
	$params['instance_id'] = $instance_item;
	$values_instance = $hotel_instances_mcs->get_flagged_instance($params);
	$values_instance = $values_instance[0];
	unset($params);


?>
<fieldset><legend>Instance Details</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200">Name </td>
    <td><?= $values_instance['hotel_name'] ?></td>
  </tr>
  <tr>
    <td>destination_airport</td>
    <td><?= $values_instance['destination_airport'] ?></td>
  </tr>
  <tr>
    <td>tour_op</td>
    <td><?= $values_instance['tour_op'] ?></td>
  </tr>
  <tr>
    <td>resort</td>
    <td><?= $values_instance['resort'] ?></td>
  </tr>
  <tr>
    <td>travel_code</td>
    <td><?= $values_instance['travel_code'] ?></td>
</tr>
<tr>
    <td>resort_code</td>
    <td><?= $values_instance['resort_code'] ?></td>
</tr>
<tr>
    <td>holiday_code</td>
    <td><?= $values_instance['holiday_code'] ?></td>
</tr>
<tr>
    <td>board_basis</td>
    <td><?= $values_instance['board_basis'] ?></td>
</tr>
<tr>
    <td>hotel_code</td>
    <td><?= $values_instance['hotel_code'] ?></td>
</tr>
<tr>
    <td>instance_date</td>
    <td><?= date('d/m/y',$values['instance_date']) ?></td>
</tr>

</table>

<br />

<? } ?>

<h3>With the following accommodation:</h3>


<fieldset><legend>Hotel Details</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200">Name</td>
    <td><?= $values_acc['name'] ?></td>
  </tr>
  <tr>
    <td>Description</td>
    <td><?= nl2br($values_acc['description']) ?></td>
  </tr>
  <tr>
    <td>Resort</td>
    <td><?
	
		$params['tree_id'] = $values_acc['parent_id'];
		$resort_values = $destinations->get_destination($params);
		unset($params);
		echo $resort_values['name']." (".$resort_values['tree_id'].")";
	 
	 ?></td>
  </tr>
  <tr>
    <td>Photos</td>
    <td><?= count($photos->get_photos($values_acc['tree_id'])); ?></td>
  </tr>
  <tr>
    <td>Notes</td>
    <td <? if($values_acc['notes']) echo "style='border:1px solid #000'"; ?>><?= $values_acc['notes']; ?></td>
  </tr>
</table>

<br />


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">

	<input type="hidden" name="action" value="batch" />
    
	<input type="hidden" name="batch" value="<?= $_POST['batch'] ?>" />
	<input type="hidden" name="tree_id" value="<?= $_POST['tree_id'] ?>" />

    <input type="button" value="Cancel" onClick="history.go(-1)" />
    <input type="submit" name="submit" value="Yes" class="form_button" /></td>


</form>

</div>

<?



}







switch ($_REQUEST['action']) {

	default;
	case "search";
		$template->head_control();
		display_home();
		$template->foot_control();
		
	break;

	case "view";
		$template->head_control();
        display_instance();
		$template->foot_control();
	break;
	

	case "append_confirm";
		$template->head_control();
        display_append_confirm();
		$template->foot_control();
	break;

	case "append";
	
		$hotel_instances_mcs = new hotel_instances_mcs();
		
		$params = array();
		$params['tree_id'] = $_REQUEST['tree_id'];
		$params['instance_id'] = $_REQUEST['instance_id'];
		
		$hotel_instances_mcs->link_instance($params);
	
		$template->head_control();
		display_home($message);
		$template->foot_control();
		
	break;
	
	case "batch_confirm";
		$template->head_control();
        display_batch_confirm();
		$template->foot_control();
	break;

	case "batch";
	
		$hotel_instances_mcs = new hotel_instances_mcs();
		
		$params = array();
		$params['tree_id'] = $_REQUEST['tree_id'];

		
	// Get instance details
	$batch = explode(',',$_POST['batch']);
	
	
	foreach($batch as $instance_item){
	
		$params['instance_id'] = $instance_item;
		
		$hotel_instances_mcs->link_instance($params);
		
	}
	
		$template->head_control();
		display_home($message);
		$template->foot_control();
		
	break;
	
	
	case "delete_linked":
		$hotel_instances_mcs = new hotel_instances_mcs();
		$params = array();
		$params['instance_id'] = $_GET['instance_id'];
		$hotel_instances_mcs->deleted_linked_instance($params);
		header('Location:hotels.php?tree_id='.$_GET['tree_id'].'&action=view');
	break;


}



?>